package com.example.ice.fittime.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

import com.example.ice.fittime.entry.UserDetail;

import com.example.ice.fittime.entry.CommentUser;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "COMMENT_USER".
*/
public class CommentUserDao extends AbstractDao<CommentUser, String> {

    public static final String TABLENAME = "COMMENT_USER";

    /**
     * Properties of entity CommentUser.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property User_id = new Property(1, String.class, "user_id", false, "USER_ID");
        public final static Property Activity_id = new Property(2, String.class, "activity_id", false, "ACTIVITY_ID");
        public final static Property Reply_id = new Property(3, String.class, "reply_id", false, "REPLY_ID");
        public final static Property Reply_user_id = new Property(4, String.class, "reply_user_id", false, "REPLY_USER_ID");
        public final static Property Content = new Property(5, String.class, "content", false, "CONTENT");
        public final static Property Create_time = new Property(6, String.class, "create_time", false, "CREATE_TIME");
        public final static Property Update_time = new Property(7, String.class, "update_time", false, "UPDATE_TIME");
        public final static Property Link_id = new Property(8, String.class, "link_id", false, "LINK_ID");
        public final static Property Reply_user_name = new Property(9, String.class, "reply_user_name", false, "REPLY_USER_NAME");
        public final static Property User_s = new Property(10, String.class, "user_s", false, "USER_S");
        public final static Property Comment_s = new Property(11, String.class, "comment_s", false, "COMMENT_S");
    }

    private DaoSession daoSession;

    private Query<CommentUser> userActivity_CommentQuery;

    public CommentUserDao(DaoConfig config) {
        super(config);
    }
    
    public CommentUserDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"COMMENT_USER\" (" + //
                "\"ID\" TEXT PRIMARY KEY NOT NULL ," + // 0: id
                "\"USER_ID\" TEXT," + // 1: user_id
                "\"ACTIVITY_ID\" TEXT," + // 2: activity_id
                "\"REPLY_ID\" TEXT," + // 3: reply_id
                "\"REPLY_USER_ID\" TEXT," + // 4: reply_user_id
                "\"CONTENT\" TEXT," + // 5: content
                "\"CREATE_TIME\" TEXT," + // 6: create_time
                "\"UPDATE_TIME\" TEXT," + // 7: update_time
                "\"LINK_ID\" TEXT," + // 8: link_id
                "\"REPLY_USER_NAME\" TEXT," + // 9: reply_user_name
                "\"USER_S\" TEXT," + // 10: user_s
                "\"COMMENT_S\" TEXT);"); // 11: comment_s
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"COMMENT_USER\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, CommentUser entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.getId());
 
        String user_id = entity.getUser_id();
        if (user_id != null) {
            stmt.bindString(2, user_id);
        }
 
        String activity_id = entity.getActivity_id();
        if (activity_id != null) {
            stmt.bindString(3, activity_id);
        }
 
        String reply_id = entity.getReply_id();
        if (reply_id != null) {
            stmt.bindString(4, reply_id);
        }
 
        String reply_user_id = entity.getReply_user_id();
        if (reply_user_id != null) {
            stmt.bindString(5, reply_user_id);
        }
 
        String content = entity.getContent();
        if (content != null) {
            stmt.bindString(6, content);
        }
 
        String create_time = entity.getCreate_time();
        if (create_time != null) {
            stmt.bindString(7, create_time);
        }
 
        String update_time = entity.getUpdate_time();
        if (update_time != null) {
            stmt.bindString(8, update_time);
        }
 
        String link_id = entity.getLink_id();
        if (link_id != null) {
            stmt.bindString(9, link_id);
        }
 
        String reply_user_name = entity.getReply_user_name();
        if (reply_user_name != null) {
            stmt.bindString(10, reply_user_name);
        }
 
        String user_s = entity.getUser_s();
        if (user_s != null) {
            stmt.bindString(11, user_s);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, CommentUser entity) {
        stmt.clearBindings();
        stmt.bindString(1, entity.getId());
 
        String user_id = entity.getUser_id();
        if (user_id != null) {
            stmt.bindString(2, user_id);
        }
 
        String activity_id = entity.getActivity_id();
        if (activity_id != null) {
            stmt.bindString(3, activity_id);
        }
 
        String reply_id = entity.getReply_id();
        if (reply_id != null) {
            stmt.bindString(4, reply_id);
        }
 
        String reply_user_id = entity.getReply_user_id();
        if (reply_user_id != null) {
            stmt.bindString(5, reply_user_id);
        }
 
        String content = entity.getContent();
        if (content != null) {
            stmt.bindString(6, content);
        }
 
        String create_time = entity.getCreate_time();
        if (create_time != null) {
            stmt.bindString(7, create_time);
        }
 
        String update_time = entity.getUpdate_time();
        if (update_time != null) {
            stmt.bindString(8, update_time);
        }
 
        String link_id = entity.getLink_id();
        if (link_id != null) {
            stmt.bindString(9, link_id);
        }
 
        String reply_user_name = entity.getReply_user_name();
        if (reply_user_name != null) {
            stmt.bindString(10, reply_user_name);
        }
 
        String user_s = entity.getUser_s();
        if (user_s != null) {
            stmt.bindString(11, user_s);
        }
    }

    @Override
    protected final void attachEntity(CommentUser entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.getString(offset + 0);
    }    

    @Override
    public CommentUser readEntity(Cursor cursor, int offset) {
        CommentUser entity = new CommentUser( //
            cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // user_id
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // activity_id
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // reply_id
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // reply_user_id
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // content
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // create_time
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // update_time
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // link_id
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // reply_user_name
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10) // user_s
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, CommentUser entity, int offset) {
        entity.setId(cursor.getString(offset + 0));
        entity.setUser_id(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setActivity_id(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setReply_id(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setReply_user_id(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setContent(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setCreate_time(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setUpdate_time(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setLink_id(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setReply_user_name(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setUser_s(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
     }
    
    @Override
    protected final String updateKeyAfterInsert(CommentUser entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(CommentUser entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(CommentUser entity) {
        throw new UnsupportedOperationException("Unsupported for entities with a non-null key");
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "comment" to-many relationship of UserActivity. */
    public List<CommentUser> _queryUserActivity_Comment(String comment_s) {
        synchronized (this) {
            if (userActivity_CommentQuery == null) {
                QueryBuilder<CommentUser> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Comment_s.eq(null));
                userActivity_CommentQuery = queryBuilder.build();
            }
        }
        Query<CommentUser> query = userActivity_CommentQuery.forCurrentThread();
        query.setParameter(0, comment_s);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserDetailDao().getAllColumns());
            builder.append(" FROM COMMENT_USER T");
            builder.append(" LEFT JOIN USER_DETAIL T0 ON T.\"USER_S\"=T0.\"USER_ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected CommentUser loadCurrentDeep(Cursor cursor, boolean lock) {
        CommentUser entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        UserDetail user = loadCurrentOther(daoSession.getUserDetailDao(), cursor, offset);
        entity.setUser(user);

        return entity;    
    }

    public CommentUser loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<CommentUser> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<CommentUser> list = new ArrayList<CommentUser>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<CommentUser> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<CommentUser> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
